<?php
declare (strict_types = 1);

namespace app\admin\command;

use app\common\model\Matches;
use app\common\model\MatchesLineup;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class LineUp extends Command
{
    //赛事阵容  同文字直播及指数   对应的赛事拉取后 执行此定时器
    protected function configure()
    {
        // 指令配置
        $this->setName('lineup')
            ->addArgument('action', Argument::OPTIONAL, '参数')
            ->setDescription('比赛阵容');
    }

    protected function execute(Input $input, Output $output)
    {
        $action = trim($input->getArgument('action'));
        $start = strtotime(date('Y-m-d', time()) . '00:00:00');
        $end = strtotime(date('Y-m-d', time()) . '23:59:59');
        Matches::field('id,api_id,match_id,home_team_name as name,type')->where(['type' => $action])->whereTime('match_time', 'between', [$start, $end])->chunk(100, function ($matches) use ($action, $output) {
            foreach ($matches as $key => $matche) {
                $output->writeln('=========================循环第' . ($key + 1) . '个数据，开始循环赛事名称为:' . $matche['name'] . '=====================================');
                // 处理结果集...
                if ($action == 'football') {
                    $match_id = $matche['match_id'];
                } else {
                    $match_id = $matche['api_id'];
                }
                $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/match/lineup?match_id=' . $match_id;
                $res = curl_get($url);
                $arr = json_decode($res, true);
                if(!empty($arr)){
                    if ($arr['code'] == 0 && count($arr['data']) > 0) {
                        if (array_key_exists('home', $arr['data']['sub']) && !empty($arr['data']['sub']['home'])) {
                            $sub_home = json_encode($arr['data']['sub']['home'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                        } else {
                            $sub_home = NULL;
                        }
                        if (array_key_exists('away', $arr['data']['sub']) && !empty($arr['data']['sub']['away'])) {
                            $sub_away = json_encode($arr['data']['sub']['away'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                        } else {
                            $sub_away = NULL;
                        }
                        if (array_key_exists('home', $arr['data']['first']) && !empty($arr['data']['first']['home'])) {
                            $first_home = json_encode($arr['data']['first']['home'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                        } else {
                            $first_home = NULL;
                        }
                        if (array_key_exists('away', $arr['data']['first']) && !empty($arr['data']['first']['away'])) {
                            $first_away = json_encode($arr['data']['first']['away'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                        } else {
                            $first_away = NULL;
                        }
                        if (array_key_exists('injury', $arr['data']) && !empty($arr['data']['injury'])) {
                            $injury = json_encode($arr['data']['injury'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                        } else {
                            $injury = NULL;
                        }
                        if (array_key_exists('home_coach', $arr['data']) && !empty($arr['data']['home_coach'])) {
                            $home_coach = json_encode($arr['data']['home_coach'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                        } else {
                            $home_coach = NULL;
                        }
                        if (array_key_exists('away_coach', $arr['data']) && !empty($arr['data']['away_coach'])) {
                            $away_coach = json_encode($arr['data']['away_coach'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                        } else {
                            $away_coach = NULL;
                        }
                        $away_market_value = $arr['data']['away_market_value'] ?? 0;
                        $home_market_value = $arr['data']['home_market_value'] ?? 0;
                        $away_formation = $arr['data']['away_formation'] ?? '';
                        $home_formation = $arr['data']['home_formation'] ?? '';
                        $result_arr = [
                            'sub_home' => $sub_home,
                            'sub_away' => $sub_away,
                            'first_home' => $first_home,
                            'first_away' => $first_away,
                            'injury' => $injury,
                            'home_coach' => $home_coach,
                            'away_coach' => $away_coach,
                            'away_market_value' => $away_market_value,
                            'home_market_value' => $home_market_value,
                            'away_formation' => $away_formation,
                            'home_formation' => $home_formation,
                            'match_id'=>$match_id,
                            'foreign_key_id' => $matche->id,
                            'type' => $action
                        ];
                        $is_find = MatchesLineup::where(['foreign_key_id' => $matche->id])->find();
                        if (empty($is_find)) {
                            MatchesLineup::create($result_arr);
                            $str = 'add';
                        } else {
                            //更新
                            $is_find->save($result_arr);
                            $str = 'update';
                        }
                        $output->writeln('=========================' . $str . '完成=====================================');
                    }else {
                        $output->writeln('api参数出错');
                    }
                }
               
            }
        });
        // 指令输出
        $output->writeln('=========================结束===============================');
    }
}
